{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.5 LightGBM算法案例实战1 - 客户违约预测模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**10.5.1 案例背景**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "银行等金融机构经常会根据客户的个人资料、财产等情况，来预测借款客户是否会违约，从而进行贷前审核，贷中管理，贷后违约处理等工作。金融处理的就是风险，需要在风险和收益间寻求到一个平衡点，现代金融某种程度上便是一个风险定价的过程，通过个人的海量数据，从而对其进行风险评估并进行合适的借款利率定价，这便是一个典型的风险定价过程，这也被称之为大数据风控。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**10.5.2 模型搭建**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:14.410344Z",
     "start_time": "2020-11-21T02:44:13.259703Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收入</th>\n",
       "      <th>年龄</th>\n",
       "      <th>性别</th>\n",
       "      <th>历史授信额度</th>\n",
       "      <th>历史违约次数</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>462087</td>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>362324</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>13583</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>332011</td>\n",
       "      <td>52</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>252895</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>352355</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       收入  年龄  性别  历史授信额度  历史违约次数  是否违约\n",
       "0  462087  26   1       0       1     1\n",
       "1  362324  32   0   13583       0     1\n",
       "2  332011  52   1       0       1     1\n",
       "3  252895  39   0       0       1     1\n",
       "4  352355  50   1       0       0     1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.读取数据\n",
    "import pandas as pd\n",
    "df = pd.read_excel('客户信息及违约表现.xlsx')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:18.983862Z",
     "start_time": "2020-11-21T02:44:17.344483Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier()"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.提取特征变量和目标变量\n",
    "X = df.drop(columns='是否违约')\n",
    "Y = df['是否违约']\n",
    "\n",
    "# 3.划分训练集和测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=123)\n",
    "\n",
    "# 4.模型训练及搭建\n",
    "from lightgbm import LGBMClassifier\n",
    "model = LGBMClassifier()\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过如下代码可以查看官方讲解\n",
    "# LGBMClassifier?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**10.5.3 模型预测及评估**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:21.200235Z",
     "start_time": "2020-11-21T02:44:21.189264Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 1 0 1 0 1 0 0 0 1 1 1 0 1 0 1 1 0 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 1 0\n",
      " 1 1 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 1 1\n",
      " 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1 1 0 0 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 1 1 0\n",
      " 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 1\n",
      " 0 1 0 1 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0\n",
      " 0 0 1 1 0 1 0 0 1 1 0 1 0 1 1]\n"
     ]
    }
   ],
   "source": [
    "# 预测测试集数据\n",
    "y_pred = model.predict(X_test)\n",
    "print(y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:23.099734Z",
     "start_time": "2020-11-21T02:44:23.088737Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>预测值</th>\n",
       "      <th>实际值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   预测值  实际值\n",
       "0    1    1\n",
       "1    0    1\n",
       "2    1    1\n",
       "3    0    0\n",
       "4    1    1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测值和实际值对比\n",
    "a = pd.DataFrame()  # 创建一个空DataFrame \n",
    "a['预测值'] = list(y_pred)\n",
    "a['实际值'] = list(y_test)\n",
    "a.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:25.573234Z",
     "start_time": "2020-11-21T02:44:25.567250Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.78"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:26.484901Z",
     "start_time": "2020-11-21T02:44:26.473935Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.78"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看得分\n",
    "model.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:28.400306Z",
     "start_time": "2020-11-21T02:44:27.720106Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAPcklEQVR4nO3db4ilZ3nH8e+vuwYqGiPuKHGT7aYlalcwomOipbaxYs2myCIIJoqhQVlDjfRlQqHmhW8U34gY3S4hBKEaoYpZy2ooFE0hJmYDa8xuiEwjbiYJZKNiIL4Im1x9MZNymD2z55nZ5/y7z/cDA/M8zz1nrptZfrlyP/9SVUiS5t+fTLsASVI/DHRJaoSBLkmNMNAlqREGuiQ1Yue0fvGuXbtq79690/r1kjSXHn744eeqamnYsakF+t69ezl27Ni0fr0kzaUkv9nsmEsuktQIA12SGmGgS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/Ang+1V1CqCq5n3eXeZcwGuTBHgNa4F+ZrJl9qeq7mNtDpvpPb9mOdA3e/H0VsfMk63O59Os/Rd+no2cc5LdwEeBQ7Shy9/5LcDrk/wkycNJbphYdePRZc5fB/6StddX/hL456p6eTLlTUXv+dXpBRdT0tvLqedI5/kk+QBrgf7XY61o/LrM+avALVX10lrzNve6zHkn8G7gg8CfAj9L8kBV/WrcxY1Jlzl/GDgO/B3wF8B/Jfmfqnp+3MVNSe/5NcuBvogvp+40nyTvAO4A9lfVbydU27h0mfMycPd6mO8Crk1ypqp+MJkSe9f13/ZzVfUC8EKS+4ArgHkN9C5zvhH4Uq0tMK8k+TXwNuDnkylx4nrPr1leclnEl1OPnHOSPcD3gU/Ncbc2aOScq+qyqtpbVXuB/wD+aY7DHLr9274HeH+SnUleDVwFPDbhOvvUZc6nWPs/EpK8CXgr8MREq5ys3vNrZjv0WsCXU3ec8xeANwDfWO9Yz9QcP6mu45yb0mXOVfVYkh8DjwAvA3dU1dDL3+ZBx7/zF4G7kvySteWIW6pqbh+rm+Q7wNXAriSrwG3Aq2B8+eWt/5LUiFlecpEkbYGBLkmNMNAlqREGuiQ1wkCXpEYY6JLUCANdkhrxf/MRHM9SosPUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查看预测属于各个分类的概率\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "\n",
    "# 绘制ROC曲线\n",
    "from sklearn.metrics import roc_curve\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(fpr, tpr)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:29.975095Z",
     "start_time": "2020-11-21T02:44:29.958999Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8221950971416945"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# AUC值\n",
    "from sklearn.metrics import roc_auc_score\n",
    "score = roc_auc_score(y_test, y_pred_proba[:,1])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:32.165408Z",
     "start_time": "2020-11-21T02:44:32.159427Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1175,  668,  118,  895,  126])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征重要性\n",
    "model.feature_importances_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-21T02:44:44.135407Z",
     "start_time": "2020-11-21T02:44:44.120447Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>特征名称</th>\n",
       "      <th>特征重要性</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>收入</td>\n",
       "      <td>1175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>历史授信额度</td>\n",
       "      <td>895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>年龄</td>\n",
       "      <td>668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>历史违约次数</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>性别</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     特征名称  特征重要性\n",
       "0      收入   1175\n",
       "3  历史授信额度    895\n",
       "1      年龄    668\n",
       "4  历史违约次数    126\n",
       "2      性别    118"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features = X.columns  # 获取特征名称\n",
    "importances = model.feature_importances_  # 获取特征重要性\n",
    "\n",
    "# 通过二维表格形式显示\n",
    "importances_df = pd.DataFrame()\n",
    "importances_df['特征名称'] = features\n",
    "importances_df['特征重要性'] = importances\n",
    "importances_df.sort_values('特征重要性', ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**10.5.4 模型参数调优**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-11T09:33:31.506709Z",
     "start_time": "2020-11-11T09:33:31.501723Z"
    }
   },
   "outputs": [],
   "source": [
    "# 参数调优\n",
    "from sklearn.model_selection import GridSearchCV  # 网格搜索合适的超参数\n",
    "parameters = {'num_leaves': [10, 15, 31], 'n_estimators': [10, 20, 30], 'learning_rate': [0.05, 0.1, 0.2]}\n",
    "model = LGBMClassifier()  # 构建分类器\n",
    "grid_search = GridSearchCV(model, parameters, scoring='roc_auc', cv=5)  # cv=5表示交叉验证5次，scoring='roc_auc'表示以ROC曲线的AUC评分作为模型评价准则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-11T09:33:34.688267Z",
     "start_time": "2020-11-11T09:33:32.649672Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'learning_rate': 0.2, 'n_estimators': 10, 'num_leaves': 10}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输出参数最优值\n",
    "grid_search.fit(X_train, y_train)  # 传入数据\n",
    "grid_search.best_params_  # 输出参数的最优值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-11T09:33:53.026960Z",
     "start_time": "2020-11-11T09:33:52.992545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(learning_rate=0.2, n_estimators=10, num_leaves=10)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重新搭建分类器\n",
    "model = LGBMClassifier(num_leaves=10, n_estimators=10,learning_rate=0.2)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-11T09:33:55.345950Z",
     "start_time": "2020-11-11T09:33:55.213275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAWKUlEQVR4nO3de2zd5Z3n8fc3dhJydQJxbnZuQAKEXFpioCrQ0jK0wFwoEqsBuu0WlYnQFvaqFdWuOjMSq9nOtLNqq8JkI0RbNLODVjO9ZGbSYdpOh1YLlIQFQgKFhiQQJzRxSOIkDnFi+7t/2Ekd48THybGPz8/vl2Th3/k955zvg51Pnjzn93ueyEwkSdVvTKULkCSVh4EuSQVhoEtSQRjoklQQBrokFURtpd54xowZuXDhwkq9vSRVpR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kRsiYinh7vGcivhd7suIv4uIl7u6XNVr9oaEY9HxN6I2HyG8+XPr8wckV90L9X7JnAxMA54GVjap81twA/p3jHpQ8AvKl33MPT5w8D0nu9vHQ197tXun+le9fPOStc9DD/nacCrwPye45mVrnsY+vxfgT/t+b4e2A+Mq3Tt59HnjwBXAZvPcL7s+TWSR+ijcXPqAfucmc9k5oGew+fo3h2qmpXycwZ4EPhbYO9wFjdESunzPcB3M/NtgMys9n6X0ucEpkREAJPpDvSO4S2zfDLzZ3T34UzKnl8jOdDLtjl1FRlsfz5P99/w1WzAPkdEA3AHsIZiKOXnvASYHhH/EhEvRMRnh626oVFKn78JXEH39pWvAP8+M7uGp7yKKHt+lbTBRYWUbXPqKlJyfyLiY3QH+vVDWtHQK6XPXwMeyszO7sFb1Sulz7XAKuAmYALwbEQ8l5lvDHVxQ6SUPn8SeAn4OHAJ8KOI+HlmHhrq4iqk7Pk1kgN9NG5OXVJ/ImIF8Bhwa2a+O0y1DZVS+twEPNkT5jOA2yKiIzO/Pzwlll2pv9v7MrMNaIuInwErgWoN9FL6fC/w5eyeYN4aEduBy4Hnh6fEYVf2/BrJUy6jcXPqAfscEfOB7wKfqeLRWm8D9jkzF2XmwsxcCPwN8G+rOMyhtN/tHwA3RERtREwErgVeG+Y6y6mUPr9N979IiIhZwGXAtmGtcniVPb9G7Ag9R+Hm1CX2+Q+Bi4BHe0asHVnFK9WV2OdCKaXPmflaRPwjsAnoAh7LzH4vf6sGJf6cHwa+HRGv0D0d8VBmVu2yuhHx18CNwIyIaAb+CBgLQ5df3vovSQUxkqdcJEmDYKBLUkEY6JJUEAa6JBWEgS5JBWGgS1JBGOiSVBD/H5LdsMQCyQMyAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查看ROC曲线\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "from sklearn.metrics import roc_curve\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(fpr, tpr)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-11T09:33:56.576606Z",
     "start_time": "2020-11-11T09:33:56.565636Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8712236801953005"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看AUC值\n",
    "y_pred_proba = model.predict_proba(X_test)\n",
    "from sklearn.metrics import roc_auc_score\n",
    "score = roc_auc_score(y_test, y_pred_proba[:, 1])\n",
    "score"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
